import { createRouter, createWebHistory } from 'vue-router'


const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home/:id?',
    name: 'Home',
    component:  () => import('@/views/home/index.vue'), 
    meta: {
      title: '网站首页',
      showOnTab: true
    }
  },
  {
    path: '/about/:id?',
    name: 'About',
    component: () => import('@/views/about/index.vue'),
    meta: {
      title: '公司简介',
      showOnTab: true
    }
  },
 
  {
    path: '/products/:id?',
    name: 'Products',
    component: () => import('@/views/products/index.vue'),
    meta: {
      title: '产品中心',
      showOnTab: true
    }
  },
  {
    path: '/cases/:id?',
    name: 'Cases',
    component: () => import('@/views/cases/index.vue'),
    meta: {
      title: '合作案例',
      showOnTab: true,
      defaultId: '23'
    }
  },
  {
    path: '/news/:id?',
    name: 'News',
    component: () => import('@/views/news/index.vue'),
    meta: {
      title: '新闻中心',
      showOnTab: true,
      defaultId: '24'
    }
  },
  {
    path: '/talent/:id?',
    name: 'Talent',
    component: () => import('@/views/talent/index.vue'),
    meta: {
      title: '人才招聘',
      showOnTab: true,
      defaultId: '25'
    }
  },
  {
    path: '/contact/:id?',
    name: 'Contact',
    component: () => import('@/views/contact/index.vue'),
    meta: {
      title: '联系我们',
      showOnTab: true,
      defaultId: '26'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 路由守卫，设置页面标题
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router 